#include <chrono>
#include <random>

#include <raylib.h>
#include <raymath.h>
#define RAYGUI_IMPLEMENTATION
#include <raygui.h>

#include "include/datasingleton.h"
#include "include/FireworkManager.h"

int main(void) {

	InitAudioDevice();
	//启用反锯齿
	SetConfigFlags(FLAG_MSAA_4X_HINT);

	//初始化窗口
	InitWindow(GetScreenWidth(), GetScreenHeight(), "Sample");

	// 初始化摄像机
	Camera3D camera = { 0 };
	camera.position = (Vector3) {
		-50.0f, -50.0f, 0.0f
	    }; //相机所在位置{x,y,z}
	camera.target = (Vector3) {
		0.0f, 0.0f, 60.0f
	}; //相机朝向位置{x,y,z}
	camera.up = (Vector3) {
		0.0f, 0.0f, 1.0f
	}; //相机正上方朝向矢量
	camera.fovy = 70; //相机视野宽度
	camera.projection = CAMERA_PERSPECTIVE; //采用透视投影

	SetCameraMode(camera, CAMERA_FREE);

	std::random_device rd;  // 将用于获得随机数引擎的种子
	std::mt19937_64 gen(rd()); // 以 rd() 播种的标准 mersenne_twister_engine
	std::uniform_real_distribution<float> posDis{-30, 40};
	std::uniform_real_distribution<float> speedDis{10, 20};
	std::uniform_int_distribution<> countBomb{-5, 10};
	std::uniform_real_distribution<float> dragCoefficientDis{0.9, 0.99};

	FireworkManager fireworkManager;
	fireworkManager.setFireworkCount(6);

	auto start = std::chrono::steady_clock::now();

	//设置动画帧率（刷新率，fps）为30帧/秒
	SetTargetFPS(24);
	//--------------------------------------------------------------------------------------
	// 主游戏循环
	while (!WindowShouldClose()) {  //关闭窗口或者按ESC键时返回true
		UpdateCamera(&camera);
//		double time = GetTime();
//		// Move camera around the scene
//		double cameraTime = time*0.5;
//		camera.position.x = (float)cos(cameraTime)*200.0f;
//		camera.position.y = (float)sin(cameraTime)*200.0f;
		// 每次循环更新一帧
		auto end = std::chrono::steady_clock::now();
		std::chrono::nanoseconds delt = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
		start = end;
		
		fireworkManager.update(delt);
		
		BeginDrawing();
		
		DataSingleton::instance()->draw();
		
		ClearBackground(BLACK);
		//以摄像机视角绘制3d内容
		BeginMode3D(camera);

		fireworkManager.see();

		EndMode3D();
		EndDrawing();

	}
	//关闭窗口
	CloseWindow();
	CloseAudioDevice();

	return 0;
}
